/* 
*  Copyright (C) 2009-2010 WCG Project
*
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the
*  Free Software Foundation; either version 2 of the License, or (at
*  your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*  In addition, as a special exception, the author gives permission to
*  link the code of this program with the Half-Life Game Engine ("HL
*  Engine") and Modified Game Libraries ("MODs") developed by Valve,
*  L.L.C ("Valve"). You must obey the GNU General Public License in all
*  respects for all of the code used other than the HL Engine and MODs
*  from Valve. If you modify this file, you may extend this exception
*  to your version of the file, but you are not obligated to do so. If
*  you do not wish to do so, delete this exception statement from your
*  version.
*/




// Used by admin menu
new g_menuPosition[33]
new g_menuPlayers[33][32]
new g_menuPlayersNum[33]
new g_menuOption[33]
new g_menuSettings[33]



public MENU_WCGMenu( id )
{
	static pos, szMenu[256], keys
	keys = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<9)
	pos = 0

	// Add the title
	pos += formatex( szMenu[pos], 255-pos, "%L^n^n", id, "MENU_WCGMENU" )

	// Add the actual options to the menu
	pos += formatex( szMenu[pos], 255-pos, "\w1. %L^n", id, "SKILLS_MENU" )
	pos += formatex( szMenu[pos], 255-pos, "\w2. %L^n", id, "ATTRIBUTES_MENU" )
	pos += formatex( szMenu[pos], 255-pos, "\w3. %L^n", id, "CLASS_MENU" )
	pos += formatex( szMenu[pos], 255-pos, "\w4. %L^n", id, "ITEM_MENU" )
	pos += formatex( szMenu[pos], 255-pos, "\w5. %L^n", id, "HELP" )
	pos += formatex( szMenu[pos], 255-pos, "\w6. %L^n", id, "ADMIN_MENU_TITLE" )
	pos += formatex( szMenu[pos], 255-pos, "^n\w0. %L", id, "WORD_EXIT" )

	// Display the menu
	show_menu( id, keys, szMenu, -1 )

	return
}

public _MENU_WCGMenu( id, key )
{
	switch ( key )
	{
		case 0:	menu_Skill_Options( id )
		case 1:	menu_Attributes_Options( id )
		case 2:	menu_Race_Options( id )
		case 3:	menu_Item_Options( id )
		case 4:	MOTD_War3help( id )
		case 5:	menu_Admin_Options( id )
	}
	
	return PLUGIN_HANDLED
}

public menu_Skill_Options( id )
{

	new pos = 0, i, menu_body[512], menuitems[3][32]
	new keys = (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)


	formatex(menuitems[0],31,"%L",id,"SELECT_SKILLS")
	formatex(menuitems[1],31,"%L",id,"SKILLS_INFORMATION")
	formatex(menuitems[2],31,"%L",id,"RESELECT_SKILLS")

	pos += formatex(menu_body[pos], 511-pos, "%L^n^n",id,"MENU_SKILLS_OPTIONS")
	for (i = 0; i<3; i++)
	{
		pos += formatex(menu_body[pos], 511-pos, "\w%d. %s^n",i+1,menuitems[i])
	}
	pos += formatex(menu_body[pos], 511-pos, "^n^n\w9. %L",id,"BACK_STRING")
	pos += formatex(menu_body[pos], 511-pos, "^n\w0. %L",id,"WORD_EXIT")
	show_menu(id,keys,menu_body,-1)

	return PLUGIN_CONTINUE
}



public _menu_Skill_Options(id,key)
{
	switch (key){
		case 0:	MENU_SelectSkill( id )
		case 1:	MOTD_SkillsInfo( id )
		case 2:	CMD_Handle( id, "resetskills", true )
		case 8: MENU_WCGMenu(id)
		default: return PLUGIN_HANDLED
	}

	return PLUGIN_HANDLED
}


public menu_Attributes_Options( id )
{

	new pos = 0, i, menu_body[512], menuitems[3][32]
	new keys = (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)


	formatex(menuitems[0],31,"%L",id,"SELECT_ATTRIBUTES")
	formatex(menuitems[1],31,"%L",id,"ATTRIBUTES_INFORMATION")
	formatex(menuitems[2],31,"%L",id,"RESELECT_ATTRIBUTES")

	pos += formatex(menu_body[pos], 511-pos, "%L^n^n",id,"MENU_ATTRIBUTES_OPTIONS")

	for (i = 0; i<3; i++)
		pos += formatex(menu_body[pos], 511-pos, "\w%d. %s^n",i+1,menuitems[i])

	pos += formatex(menu_body[pos], 511-pos, "^n^n\w9. %L",id,"BACK_STRING")
	pos += formatex(menu_body[pos], 511-pos, "^n\w0. %L",id,"WORD_EXIT")
	show_menu(id,keys,menu_body,-1)

	return PLUGIN_CONTINUE
}


public _menu_Attributes_Options(id,key)
{
	switch (key){
		case 0:	MENU_SelectAttribute( id )
		case 1:	MOTD_AttributesInfo( id )
		case 2:	CMD_Handle( id, "resetattribs", true )
		case 8: MENU_WCGMenu(id)
		default: return PLUGIN_HANDLED
	}

	return PLUGIN_HANDLED
}



public menu_Race_Options(id){

	new pos = 0, i, menu_body[512], menuitems[4][32]
	new keys = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<8)|(1<<9)

	formatex(menuitems[0],31,"%L",id,"CHANGE_CLASS")
	formatex(menuitems[1],31,"%L",id,"SHOW_LEVEL")
	formatex(menuitems[2],31,"%L",id,"RESET_XP")
	formatex(menuitems[3],31,"%L",id,"SHOW_PLAYER_SKILLS")

	pos += formatex(menu_body[pos], 511-pos, "%L^n^n",id,"MENU_RACE_OPTIONS")
	for (i = 0; i<4; i++)
	{
		pos += formatex(menu_body[pos], 511-pos, "\w%d. %s^n",i+1,menuitems[i])
	}
	pos += formatex(menu_body[pos], 511-pos, "^n^n\w9. %L",id,"BACK_STRING")
	pos += formatex(menu_body[pos], 511-pos, "^n\w0. %L",id,"WORD_EXIT")
	show_menu(id,keys,menu_body,-1)

	return PLUGIN_CONTINUE
}

public _menu_Race_Options(id,key)
{
	switch (key){
		case 0:	WCG_ChangeClassStart( id )
		case 1:	WCG_ShowClassInfo( id )
		case 2:	MENU_ResetXP( id )
		case 3:	MOTD_PlayerSkills( id, true )
		case 8: MENU_WCGMenu(id)
		default: return PLUGIN_HANDLED
	}

	return PLUGIN_HANDLED
}

public menu_Item_Options(id){

	new pos = 0, i, menu_body[512], menuitems[4][32]
	new keys = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<8)|(1<<9)

	formatex(menuitems[0],31,"%L",id,"SHOPMENU1_OPTION")
	formatex(menuitems[1],31,"%L",id,"SHOPMENU2_OPTION")
	formatex(menuitems[2],31,"%L",id,"SHOW_SHOPMENU1_INFO")
	formatex(menuitems[3],31,"%L",id,"SHOW_SHOPMENU2_INFO")

	pos += formatex(menu_body[pos], 511-pos, "%L^n^n",id,"MENU_ITEM_OPTIONS")
	for (i = 0; i<4; i++)
	{
		pos += formatex(menu_body[pos], 511-pos, "\w%d. %s^n",i+1,menuitems[i])
	}
	pos += formatex(menu_body[pos], 511-pos, "^n^n\w9. %L",id,"BACK_STRING")
	pos += formatex(menu_body[pos], 511-pos, "^n\w0. %L",id,"WORD_EXIT")
	show_menu(id,keys,menu_body,-1)

	return PLUGIN_CONTINUE
}

public _menu_Item_Options(id,key)
{
	switch (key){
		case 0:	MENU_Shopmenu( id, 0 )
		case 1:	MENU_Shopmenu( id, 9 )
		case 2:	MOTD_ItemsInfo( id, 0 )
		case 3:	MOTD_ItemsInfo( id, 9 )
		case 8: MENU_WCGMenu(id)
		default: return PLUGIN_HANDLED
	}

	return PLUGIN_HANDLED
}

public menu_Admin_Options(id)
{

	if ( id && !( get_user_flags( id ) & XP_GetAdminFlag() ) )
	{
		client_print(id,print_center,"%s %L",g_MODclient, id,"YOU_HAVE_NO_ACCESS")
		return PLUGIN_HANDLED
	}

	new pos = 0, i, menu_body[512], menuitems[3][32]
	new keys = (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)

	formatex(menuitems[0],31,"%L",id,"GIVE_IND_XP")
	formatex(menuitems[1],31,"%L",id,"GIVE_MULT_XP")
	formatex(menuitems[2],31,"%L",id,"SAVE_ALL_XP")

	pos += formatex(menu_body[pos], 511-pos, "%L^n^n",id,"MENU_ADMIN_MENU")
	for (i = 0; i<3; i++)
	{
		pos += formatex(menu_body[pos], 511-pos, "\w%d. %s^n",i+1,menuitems[i])
	}
	pos += formatex(menu_body[pos], 511-pos, "^n^n\w9. %L",id,"BACK_STRING")
	pos += formatex(menu_body[pos], 511-pos, "^n\w0. %L",id,"WORD_EXIT")
	show_menu(id,keys,menu_body,-1)

	return PLUGIN_CONTINUE
}

public _menu_Admin_Options(id,key)
{
	switch (key){
		case 0:{
			g_menuOption[id] = 1
			g_menuSettings[id] = 50
			menu_PlayerXP_Options(id,g_menuPosition[id] = 0)
		}
		case 1:{
			g_menuOption[id] = 1
			g_menuSettings[id] = 50
			menu_TeamXP_Options(id)
		}
		case 2: DB_SaveAll( false )
		case 8: MENU_WCGMenu(id)
	}

	return PLUGIN_HANDLED
}

public menu_PlayerXP_Options(id,pos){

	if (pos < 0){
		menu_Admin_Options(id)
		return PLUGIN_CONTINUE
	}

	get_players(g_menuPlayers[id],g_menuPlayersNum[id])

	new menuBody[512]
	new b = 0
	new i
	new name[32], team[4], title[128], back[16], exitstring[16]
	new start = pos * 7

	if (start >= g_menuPlayersNum[id])
		start = pos = g_menuPosition[id] = 0

	formatex(title,127,"%L",id,"MENU_GIVE_PLAYERS_XP")

	new len = formatex(menuBody,511, "%s\R%d/%d^n\w^n",title,pos+1,(g_menuPlayersNum[id] / 7 + ((g_menuPlayersNum[id] % 7) ? 1 : 0 )))
	new end = start + 7
	new keys = (1<<9)|(1<<7)

	if (end > g_menuPlayersNum[id])
		end = g_menuPlayersNum[id]

	for(new a = start; a < end; ++a)
	{
		i = g_menuPlayers[id][a]
		get_user_name(i,name,31)
		get_user_team(i,team,3)
		keys |= (1<<b)
		len += formatex(menuBody[len],511-len,"\w%d. %s^n\w",++b,name)
	}

	formatex(title,127,"%L",id,"GIVE")
	len += formatex(menuBody[len],511-len,"^n8. %s  %d XP^n",title,g_menuSettings[id])

	formatex(back,15,"%L",id,"BACK_STRING")

	if (end != g_menuPlayersNum[id]){
		formatex(menuBody[len],511-len,"^n9. %L...^n0. %s", id,"MORE_STRING", pos ? back : back)
		keys |= (1<<8)
	}
	else{
		formatex(exitstring,15,"%L",id,"WORD_EXIT")
		formatex(menuBody[len],511-len,"^n0. %s", pos ? back : exitstring)
	}


	show_menu(id,keys,menuBody,-1)
	return PLUGIN_CONTINUE

}

public _menu_PlayerXP_Options(id,key)
{
	switch(key){
		case 7:{
			++g_menuOption[id]
			if (g_menuOption[id]>6){
				g_menuOption[id]=1
			}
			switch(g_menuOption[id]){
				case 1: g_menuSettings[id] = 50
				case 2: g_menuSettings[id] = 100
				case 3: g_menuSettings[id] = 500
				case 4: g_menuSettings[id] = 1000
				case 5: g_menuSettings[id] = 5000
				case 6: g_menuSettings[id] = 10000
			}
			menu_PlayerXP_Options(id,g_menuPosition[id])
		}
		case 8: menu_PlayerXP_Options(id,++g_menuPosition[id])
		case 9: return PLUGIN_HANDLED
		default:{
			new player = g_menuPlayers[id][g_menuPosition[id] * 7 + key]
			client_print(player,print_chat,"%s %L",g_MODclient, id,"THE_ADMIN_JUST_GAVE_YOU_XP",g_menuSettings[id])
			PlayerInfo[player][PI_XP] += g_menuSettings[id]

			XP_Check( player )

			menu_PlayerXP_Options(id,g_menuPosition[id])
		}
	}

	return PLUGIN_HANDLED
}

public menu_TeamXP_Options(id){

	new pos = 0, i, menu_body[512], menuitems[3][32], give[16]
	new keys = (1<<0)|(1<<1)|(1<<2)|(1<<7)|(1<<8)|(1<<9)

	formatex(menuitems[0],31,"%L",id,"TERRORISTS")
	formatex(menuitems[1],31,"%L",id,"CT")
	formatex(menuitems[2],31,"%L",id,"EVERYONE")

	pos += formatex(menu_body[pos], 511-pos, "%L^n^n",id,"MENU_TEAM_XP")
	for (i = 0; i<3; i++)
	{
		pos += formatex(menu_body[pos], 511-pos, "\w%d. %s^n",i+1,menuitems[i])
	}
	formatex(give,15,"%L",id,"GIVE")
	pos += formatex(menu_body[pos], 511-pos,"^n8. %s  %d XP^n",give,g_menuSettings[id])
	pos += formatex(menu_body[pos], 511-pos, "^n^n\w9. %L",id,"BACK_STRING")
	pos += formatex(menu_body[pos], 511-pos, "^n\w0. %L",id,"WORD_EXIT")
	show_menu(id,keys,menu_body,-1)

	return PLUGIN_CONTINUE
}

public _menu_TeamXP_Options(id,key)
{
	switch(key){
		case 0:
		{
			ADMIN_GiveXP( id, "@T", g_menuSettings[id] )
			menu_TeamXP_Options(id)
		}
		case 1:{
			ADMIN_GiveXP( id, "@CT", g_menuSettings[id] )
			menu_TeamXP_Options(id)
		}
		case 2:{
			ADMIN_GiveXP( id, "@ALL", g_menuSettings[id] )
			menu_TeamXP_Options(id)
		}
		case 7:{
			++g_menuOption[id]
			if (g_menuOption[id]>6){
				g_menuOption[id]=1
			}
			switch(g_menuOption[id]){
				case 1: g_menuSettings[id] = 50
				case 2: g_menuSettings[id] = 100
				case 3: g_menuSettings[id] = 500
				case 4: g_menuSettings[id] = 1000
				case 5: g_menuSettings[id] = 5000
				case 6: g_menuSettings[id] = 10000
			}
			menu_TeamXP_Options(id)
		}
		case 8: menu_Admin_Options(id)
	}

	return PLUGIN_HANDLED
}

public MENU_ResetXP(id)
{

	static szMenu[128]
	new keys = (1<<0)|(1<<1)|(1<<9)
	
	formatex( szMenu, 127, "%L^n^n\w1. Yes^n\w2. No", id, "MENU_RESET_XP" )

	show_menu( id, keys, szMenu, -1 )

	return
}

public _MENU_ResetXP( id, key )
{
	// User selected yes
	if ( key == 0 )
	{
		XP_Reset( id )
	}
	
	return PLUGIN_HANDLED
}

// Function will display the changerace menu
public MENU_ChangeClass( id, iRaceXP[MAX_CLASSES] )
{
	static szRaceName[MAX_CLASSES+1][64], szMenu[512], szXP[16]

	new i, pos, iKeys = 0

	// Get our class names
	for ( i = 0 ; i < MAX_CLASSES ; i++ )
		LANG_GetClassName( i + 1, id, szRaceName[i], 63 )

	pos += formatex( szMenu[pos], 512-pos, "%L", id, "MENU_SELECT_CLASS" )

	// Then add the experience column
	pos += formatex( szMenu[pos], 512-pos, "\R%L^n^n", id, "MENU_WORD_EXPERIENCE" )


	// Build the changerace menu (for every race)
	for ( i = 0 ; i < MAX_CLASSES ; i++ )
	{
		num_to_str( iRaceXP[i], szXP, 15 )
		
		// User's current race
		if ( i == PlayerInfo[id][PI_Class] - 1 )
			pos += formatex( szMenu[pos], 512-pos, "\d%d. %s\d", i + 1, szRaceName[i] )

		// Race the user wants to change to
		else if ( i == PlayerInfo[id][PI_ChangeToRace] - 1 )
			pos += formatex( szMenu[pos], 512-pos, "\r%d. %s\r", i + 1, szRaceName[i] )

		// All other cases
		else
			pos += formatex( szMenu[pos], 512-pos, "\w%d. %s\y", i + 1, szRaceName[i] )


		pos += formatex( szMenu[pos], 512-pos, "\R%s^n", szXP )

		iKeys |= (1<<i)
	}

	if( PlayerInfo[id][PI_Class] != CLASS_NONE )
	{
		pos += format( szMenu[pos], 512-pos, "^n\w0. %L", id, "WORD_CANCEL" )
		iKeys |= (1<<9)
	}
	
	// Show the menu to the user!
	show_menu( id, iKeys, szMenu, -1 )
}



public _MENU_ChangeClass( id, key )
{
	// User pressed 0 (cancel)
	if( key == 9 )
		return PLUGIN_HANDLED

	// Save the current race data before we change
	DB_SaveXP( id, false )
	
	new iRace = key + 1

	// User currently has a race
	if ( PlayerInfo[id][PI_Class] != CLASS_NONE )
	{
		// Change the user's race at the start of next round
		if ( iRace != PlayerInfo[id][PI_Class] )
		{
			client_print( id, print_center, "%L", id, "CENTER_CHANGED_NEXT" )
			PlayerInfo[id][PI_ChangeToRace] = iRace
		}

		// Do nothing
		else
		{
			PlayerInfo[id][PI_ChangeToRace] = CLASS_NONE
		}
	}

	// User doesn't have a race so give it to him!!!
	else
	{
		WC3_SetRace( id, iRace )
	}

	return PLUGIN_HANDLED
}

public MENU_ReplaceItem( id )
{

	new szMenu[512] = "", pos = 0
	new iKeys = (1<<9)|(1<<0)|(1<<1)

	// Add the menu header
	pos += formatex( szMenu[pos], 511-pos, "%L^n^n", id, "MENU_REPLACE_ITEM" )

	new szItemName[64], szItemName2[64]
	LANG_GetItemName( g_iShopMenuItems[id][ITEM_SLOT_ONE], id, szItemName, 63 )
	LANG_GetItemName( g_iShopMenuItems[id][ITEM_SLOT_TWO], id, szItemName2, 63 )

	// Add the items
	pos += formatex( szMenu[pos], 511-pos, "\w1. %s^n", szItemName )
	pos += formatex( szMenu[pos], 511-pos, "\w2. %s^n", szItemName2 )

	// Add the exit option
	pos += formatex( szMenu[pos], 511-pos, "^n\w0. %L", id, "WORD_EXIT" )

	// Show the menu
	show_menu( id, iKeys, szMenu, -1 )

	return
}

public _menu_ReplaceItem( id, iKey )
{
	if ( iKey == 9 )
		return PLUGIN_HANDLED

	// Remove item from item slot one
	if ( iKey == 0 )
		ITEM_Remove( id, ITEM_SLOT_ONE )

	// Remove item from itemslot two
	else if ( iKey == 1 )
		ITEM_Remove( id, ITEM_SLOT_TWO )

	// Then the user typed "rings"
	if ( g_iFutureItem[id] == -3 )
		ITEM_BuyRings( id )
	else
		ITEM_Buy( id, g_iFutureItem[id] )

	return PLUGIN_HANDLED
}

public MENU_Shopmenu( id, iStart )
{

	if ( !ITEM_MenuCanBuyCheck( id ) )
		return

	new szMenu[512], szItemName[64], pos = 0, i, iItemID
	new iKeys = (1<<9)

	// Add the header
	if ( iStart == 0 )
		pos += formatex( szMenu[pos], 511-pos, "%L", id, "MENU_BUY_ITEM" )

	// "Shopmenu 2"
	else if ( iStart == MAX_PAGE_ITEMS )
		pos += formatex( szMenu[pos], 511-pos, "%L", id, "MENU_BUY_ITEM2" )

	// Lets add the items to the menu!
	for ( i = 0; i < MAX_PAGE_ITEMS; i++ )
	{
		iItemID = iStart + i

		LANG_GetItemName( iItemID, id, szItemName, 63 )

		if( iItemID == 3 || iItemID == 9 || iItemID == 10 || iItemID == 11 || iItemID == 16 || iItemID == 17 )
		{
			pos += formatex( szMenu[pos], 511-pos, "\d%d. %s (Disabled)\R%d^n\d", i + 1, szItemName, ITEM_Cost( id, iItemID ) )
			continue
		}

		pos += formatex( szMenu[pos], 511-pos, "\w%d. %s\y\R%d^n", i + 1, szItemName, ITEM_Cost( id, iItemID ) )
		iKeys |= (1<<i)
	}

	pos += formatex( szMenu[pos], 511-pos, "^n\w0. %L", id, "WORD_EXIT" )

	show_menu( id, iKeys, szMenu, -1 )
}

public _MENU_Shopmenu1( id, iKey )
{
	if ( iKey == 9 )
		return PLUGIN_HANDLED

	ITEM_Buy( id, iKey )

	return PLUGIN_HANDLED
}

public _MENU_Shopmenu2( id, iKey )
{
	if ( iKey == 9 )
		return PLUGIN_HANDLED;

	iKey += MAX_PAGE_ITEMS
	
	ITEM_Buy( id, iKey )

	return PLUGIN_HANDLED
}

public MENU_SelectSkill( id )
{
	static szMsg[512], szSkillName[64], szReqLevel[32]
	
	new pos, iSkillCounter, iSkillID, iKeys = (1<<9), iSkillLevel, iSkillType, iSkillMaxLevel

	// User has no race, how can we select skills?!?
	if ( PlayerInfo[id][PI_Class] == CLASS_NONE )
	{
		//set_hudmessage(200, 100, 0, -1.0, 0.3, 0, 1.0, 5.0, 0.1, 0.2, 2)
		WCG_StatusText( id, TXT_TOP_CENTER, HUD_AUTO, "%L", id, "SELECT_CLASS_BEFORE_SKILLS" )
		return
	}

	// Lets make sure the user has some available skill points
	new skillpoints = PlayerInfo[id][PI_Level] - SM_TotalSkillPointsUsed( id )

	// Add the menu header
	pos += formatex( szMsg[pos], 512-pos, "%L", id, "MENU_SELECT_SKILL", skillpoints )

	iSkillID = SM_GetSkillByPos( id, iSkillCounter )
	while( iSkillID != SKILL_NONE )
	{
		iSkillLevel = SM_GetSkillLevel( id, iSkillID, 4 )
		iSkillMaxLevel = g_SkillMaxLevel[iSkillID]
		iSkillType = SM_GetSkillType( iSkillID )
		LANG_GetSkillName( iSkillID , id, szSkillName, 63, 1 )

		szReqLevel[0] = 0
				
		// Add the trainable skills to the menu
		if( !SM_GetSkillAvailable( id, iSkillID, szReqLevel, 31 ) )
			pos += formatex( szMsg[pos], 512-pos, "\d" )

		else if ( skillpoints <= 0 )
		{
			pos += formatex( szMsg[pos], 512-pos, "\d" )
			formatex( szReqLevel, 31, " (Req lv %d)", PlayerInfo[id][PI_Level] + 1 )
		}
		// Then the user can choose it!
		else
		{
			iKeys |= (1<<iSkillCounter)
		}

		if( iSkillType == SKILL_TYPE_ULTIMATE )
			pos += formatex( szMsg[pos], 512-pos, "^n%d. %s (Ultimate) %s\R( %d / %d )\w", \
					iSkillCounter+1, szSkillName, szReqLevel, iSkillLevel, iSkillMaxLevel )
			
		else if( iSkillType == SKILL_TYPE_ABILITY )
			pos += formatex( szMsg[pos], 512-pos, "^n%d. %s (Ability) %s\R( %d / %d )\w", \
					iSkillCounter+1, szSkillName, szReqLevel, iSkillLevel, iSkillMaxLevel )
			
		else
			pos += formatex( szMsg[pos], 512-pos, "^n%d. %s %s\R( %d / %d )\w", \
					iSkillCounter+1, szSkillName, szReqLevel, iSkillLevel, iSkillMaxLevel )

		
		iSkillCounter++
		iSkillID = SM_GetSkillByPos( id, iSkillCounter )
	}

	// Add the cancel button to the menu
	pos += formatex( szMsg[pos], 512-pos, "^n^n\w0. %L^n^n\y%d Skill points left.\w", id, "WORD_EXIT", skillpoints )

	// Show the menu!
	show_menu( id, iKeys, szMsg, -1 )
	return
}

public _MENU_SelectSkill( id, iKey )
{
	if ( iKey == 9 )
	{
		if( GetAvailableAttributePoints( id ) )
			MENU_SelectAttribute( id )

		return PLUGIN_HANDLED
	}

	// Determine which key was just selected
	new iSkillID = SM_GetSkillByPos( id, iKey )

	// Set up the skill!
	SM_SetSkill( id, iSkillID )

	// Show menu again if they have more skills to select
	if( SM_SkillsAvailable( id ) )
		MENU_SelectSkill( id )
	else if( GetAvailableAttributePoints( id ) )
		MENU_SelectAttribute( id )

	return PLUGIN_HANDLED
}


public MENU_SelectAttribute( id )
{
	static szMsg[512], szAttributeName[64]
	
	new pos, iAttribID, iAttribLevel, iKeys = (1<<9)

	// User has no race, how can we select attributes?
	if ( PlayerInfo[id][PI_Class] == CLASS_NONE )
	{
		WCG_StatusText( id, TXT_TOP_CENTER, HUD_AUTO, "%L", id, "SELECT_CLASS_BEFORE_ATTRIBUTES" )
		return
	}

	// Lets make sure the user has some available skill points
	new ap = GetAvailableAttributePoints( id )

	// Add the menu header
	pos += formatex( szMsg[pos], 512-pos, "%L", id, "MENU_SELECT_ATTRIBUTE", ap )

	for( iAttribID = 0 ; iAttribID < MAX_ATTRIBUTES ; iAttribID++ )
	{
		LANG_GetAttributeName( iAttribID , id, szAttributeName, 63, 1 )

		iAttribLevel = GetAttributeLevel( id, iAttribID )

		if( ap <= 0 || iAttribLevel >= MAX_ATTRIBUTELEVEL )
		{
			pos += formatex( szMsg[pos], 512-pos, "\d^n%d. %s\R( %d / %d  )\w", iAttribID+1, szAttributeName, iAttribLevel, MAX_ATTRIBUTELEVEL )
			continue
		}

		pos += formatex( szMsg[pos], 512-pos, "^n%d. %s\R( %d / %d  )\w", iAttribID+1, szAttributeName, iAttribLevel, MAX_ATTRIBUTELEVEL )
//		pos += formatex( szMsg[pos], 512-pos, "^n%d. %s\R/ %d )\y+%d\w(\w", iAttribID+1, szAttributeName, MAX_ATTRIBUTELEVEL, iAttribLevel )
		iKeys |= (1<<iAttribID)
	}

	// Add the cancel button to the menu
	pos += formatex( szMsg[pos], 512-pos, "^n^n\w0. %L^n^n\y%d Attribute points left.\w", id, "WORD_EXIT", ap )

	// Show the menu!
	show_menu( id, iKeys, szMsg, -1 )
	return
}


public _MENU_SelectAttribute( id, iKey )
{
	if ( iKey == 9 )
	{
		if( SM_SkillsAvailable( id ) )
			MENU_SelectSkill( id )

		return PLUGIN_HANDLED
	}

	IncreaseAttribute( id, iKey )

	// Show menu again if they have more skills to select
	if( GetAvailableAttributePoints( id ) )
		MENU_SelectAttribute( id )
	else if( SM_SkillsAvailable( id ) )
		MENU_SelectSkill( id )

	return PLUGIN_HANDLED
}

